﻿using System;
using System.IO;
using System.Collections.Generic;

namespace easysmb
{
    using easysmbrary;

    public class EasySMBPath
    {
        internal string path { get; set; }
        internal EasySMB smb { get; set; }
        public FileAttributes Attributes { get; internal set; }
        public DateTime? LastEditTime { get; internal set; }
        public DateTime? CreationTime { get; internal set; }
        public bool Hiden { get; internal set; }
        public string FullPath { get; internal set; }
        public string PathName { get; internal set; }
        internal EasySMBPath(EasySMB smb, string path)
        {
            this.smb = smb;
            this.path = path;
            this.FullPath = path;
            this.PathName = smb.read_name(path);
        }
        internal EasySMBPath(EasySMB smb, string path, FileDirectoryInformation info)
        {
            this.smb = smb;
            this.path = path;
            this.FullPath = path;
            this.PathName = smb.read_name(path);
            this.Attributes = info.FileAttributes;
            this.LastEditTime = info.LastWriteTime;
            this.CreationTime = info.CreationTime;
            this.Hiden = (info.FileAttributes & FileAttributes.Hidden) != 0;
        }
        internal EasySMBPath(EasySMB smb, string path, FileBasicInformation info)
        {
            this.smb = smb;
            this.path = path;
            this.FullPath = path;
            this.PathName = smb.read_name(path);
            this.Attributes = info.FileAttributes;
            this.LastEditTime = info.LastWriteTime;
            this.CreationTime = info.CreationTime;
            this.Hiden = (info.FileAttributes & FileAttributes.Hidden) != 0;
        }
        public EasySMBFileStream NewFile(string filename)
        {
            if (string.IsNullOrEmpty(filename)) throw new ArgumentException();
            var file = smb.NewFile(smb.combine_path(path, filename));
            return file.GetStream(FileAccess.Write, FileShare.ReadWrite);
        }
        public bool ExistFile(string filename)
        {
            var filepath = smb.combine_path(path, filename);
            return smb.IsFile(filepath);
        }
        public void Rename(string newname)
        {
            if (smb.read_name(path) == newname) return;
            smb.Rename(this.path, newname);
            this.path = smb.change_name(this.path, newname);
        }
        public void Delete()
        {
            smb.DeletePath(this.path);
        }
        public IEnumerable<EasySMBFile> LoopFiles()
        {
            return smb.LoopFiles(this.path);
        }
        public IEnumerable<EasySMBPath> LoopPaths()
        {
            return smb.LoopPaths(this.path);
        }
    }
}
